<template>
  <div class="server-main">
    <el-form :inline="true" v-model="searchParams" class="search-form">
      <el-form-item label="数据源内外部" class="form-input">
        <el-select v-model="isInside" clearable @change="getDataSourceList">
          <el-option label="外部" :value="0"></el-option>
          <el-option label="内部" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据源" class="form-input">
        <el-select v-model="selectDataSource" @change="getSourceTable">
          <el-option
            v-for="i in dataSources"
            :key="i.value"
            :label="i.label"
            :value="i.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据表" class="form-input">
        <el-select v-model="searchParams.modelId" @change="getResourceList">
          <el-option
            v-for="i in dataTable"
            :key="i.value"
            :label="i.label"
            :value="i.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="onSubmit(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">业务数据资源库</span>
        <div>
          <!-- 仅在选择内部数据源时 显示 -->
          <el-button
            v-if="isInside === 1"
            type="primary"
            size="small"
            @click="configMap"
            >导入图谱</el-button
          >
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableList"
        :cell-style="{ color: '#333333' }"
        :header-cell-style="{ background: '#F3F4F7', color: '#000000' }"
      >
        <el-table-column
          v-for="c in headerColumns"
          :key="c.key"
          align="center"
          :prop="c.key"
          min-width="100"
          show-overflow-tooltip
        >
          <template #header>
            <el-popover placement="top" trigger="hover">
              <span>{{ c.label || "暂无注释" }}</span>
              <span slot="reference">{{ c.key }}</span>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        small
        layout="total, prev, pager, next, sizes, jumper"
        :page-sizes="[10, 20, 30]"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="searchParams.pageSize"
        :pager-count="7"
        :current-page.sync="searchParams.pageNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { updateCookie } from "@/plugins/utils/auth";
export default {
  name: "BusinessDataBaseIndex",
  components: {},
  data: () => {
    return {
      isInside: 1,
      selectDataSource: null,
      searchParams: {
        modelId: null, // 数据表
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      dataSources: [], // 数据源列表
      dataTable: [], // 数据表
      headerColumns: [], // 表头信息
      tableList: [], // 数据列表
      total: 0, // 数据总条数
      loading: false,
    };
  },
  computed: {},
  created() {
    if (this.$route.query.token) {
      updateCookie("token", this.$route.query.token);
    }
    // 获取数据源列表
    this.getDataSourceList();
  },
  methods: {
    // 获取数据资源库
    getDataSourceList() {
      this.$axios
        .post(this.$api.dataConnection.businessData.getDataSourceList, {
          pageNum: 1,
          pageSize: 100,
          inside: this.isInside,
        })
        .then((res) => {
          const sourceInfo = res.data.data;
          if (Array.isArray(sourceInfo.list) && sourceInfo.list.length) {
            this.selectDataSource = sourceInfo.list[0].dataSourceId;
            this.dataSources = sourceInfo.list.map((item) => {
              return {
                label: item.dataSourceName,
                value: item.dataSourceId,
              };
            });

            // 请求数据表
            this.getSourceTable();
          } else {
            this.dataSources = [];
            this.selectDataSource = null;
            this.dataTable = [];
            this.searchParams.modelId = null;
            this.headerColumns = [];
            this.tableList = [];
            this.total = 0;
          }
        });
    },
    // 获取数据资源表
    getSourceTable() {
      this.$axios
        .post(this.$api.dataConnection.businessData.getDataSourceTableList, {
          dataSourceId: this.selectDataSource,
          pageNum: 1,
          pageSize: 100,
        })
        .then((res) => {
          const tableInfo = res.data.data;
          if (Array.isArray(tableInfo.list) && tableInfo.list.length) {
            this.searchParams.modelId = tableInfo.list[0].id;
            this.dataTable = tableInfo.list.map((item) => {
              return {
                label: item.modelName,
                value: item.id,
              };
            });

            // 请求数据表数据
            this.getResourceList();
          } else {
            this.searchParams.modelId = null;
            this.dataTable = [];
            this.headerColumns = [];
            this.tableList = [];
            this.total = 0;
          }
        });
    },
    // 动态获取数据资源表数据
    getResourceList() {
      this.loading = true;
      this.$axios
        .get(this.$api.dataConnection.businessData.getTableDataList, {
          params: this.searchParams,
        })
        .then((res) => {
          let resource = res.data.data;
          if (Array.isArray(resource.headerNameList)) {
            this.transHeaders(
              resource.headerNameList,
              resource.commentNameList
            );
            if (Array.isArray(resource.valueList)) {
              this.transTableData(resource.headerNameList, resource.valueList);
              this.total = resource.total;
            } else {
              this.tableList = [];
              this.total = 0;
            }
          } else {
            this.headerColumns = [];
            this.total = 0;
          }
        })
        .catch((err) => {
          console.error(err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 处理表头
    transHeaders(headers, names) {
      let arr = [];
      if (headers.length) {
        for (let i = 0; i < headers.length; i++) {
          arr.push({ key: headers[i], label: names[i] });
        }
      }
      this.headerColumns = arr;
    },
    // 处理数据
    transTableData(headers, data) {
      let arr = [];
      if (data.length) {
        for (let i = 0; i < data.length; i++) {
          let item = {};
          let dataItem = data[i];
          for (let k = 0; k < headers.length; k++) {
            item[headers[k]] = dataItem[k];
          }
          arr.push(item);
        }
      }
      this.tableList = arr;
    },
    // 查询
    onSubmit(pageNum) {
      this.searchParams.pageNum = pageNum ? pageNum : this.searchParams.pageNum;
      this.getResourceList();
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.getResourceList();
    },
    // 修改每页条数
    handleSizeChange(e) {
      this.getResourceList();
    },
    // 导入图谱
    configMap() {
      if (!this.selectDataSource) {
        return this.$message.warning("没有选择数据源，无法进行导入图谱配置！");
      }
      if (!this.searchParams.modelId) {
        return this.$message.warning("没有选择数据表，无法进行导入图谱配置！");
      }
      const table = this.dataTable.find(i => i.value == this.searchParams.modelId)
      this.$router.push({
        path: "/dataConnection/businessDataBase/importToGraph",
        query: { modelId: this.searchParams.modelId, dataSourceId: this.selectDataSource, dataTable: table.label },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.server-main {
  position: relative;

  .search-form {
    padding: 16px 32px;
    border-radius: 4px;
    background: #ffffff;

    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;

      /deep/ .el-input__inner {
        width: 240px;
        height: 32px;
        background: #ffffff;
        border-radius: 2px;
        border: 1px solid #e0e0e0;
      }
    }
  }

  .el-select {
    /deep/ .el-input__inner {
      width: 240px;
      height: 32px;
      background: #ffffff;
      border-radius: 2px;
      border: 1px solid #e0e0e0;
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    border-radius: 4px;
    background: #ffffff;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
      }

      margin-bottom: 10px;
    }

    .little-circle-blue {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #55c898;
      position: relative;
      right: 4px;
      top: -2px;
    }

    .little-circle-red {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #ee5564;
      position: relative;
      right: 6px;
      top: -2px;
    }

    .page-wrapper {
      margin-top: 28px;
      display: flex;
      justify-content: flex-end;
      color: rgba(0, 0, 0, 0.65);

      .data-counter {
        font-weight: 400;
        line-height: 32px;
        margin-right: 24px;
      }

      /deep/ .btn-prev,
      /deep/ .btn-next {
        margin: 0 4px;
        min-width: 28px;
        border-radius: 2px;
        border: 1px solid rgba(0, 0, 0, 0.15);
        padding: 0;
      }

      /deep/ .el-pager .number {
        min-width: 28px;
        height: 28px;
        border-radius: 2px;
        border: 1px solid rgba(0, 0, 0, 0.15);
        margin: 0 4px;
        cursor: pointer;
      }
    }
  }

  .ontology-edit {
    padding: 0 10px;

    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  /deep/ .el-table__append-wrapper {
    padding: 10px;
  }

  .el-pagination {
    float: right;
    margin-top: 20px;

    .form-input /deep/ .el-input {
      width: 200px;
    }

    /deep/ .el-input__inner {
      height: 22px;
    }
  }

  /deep/ .el-dialog {
    border-radius: 5px;
  }
}
</style>
